import Vue from 'vue'
import Router from 'vue-router'
import bookCity from '@/views/bookCity'
import bookShelf from '@/views/bookShelf'
import detail from '@/views/detail'
import login from '@/views/login'
import read from '@/views/read'

Vue.use(Router)

let router =  new Router({
    mode:'history',
  routes: [
    {
        path: '/',
        redirect:'/bookCity'
    },
    {
        path:'/bookCity',
        name:'bookCity',
        component:bookCity
    },
    {
        path:'/bookShelf',
        name:'bookShelf',
        component:bookShelf
    },
    {
        path:'/detail/:fiction_id',
        name:'detail',
        component:detail
    },
    {
        path:'/login',
        name:'login',
        component:login
    },
    {
        path:'/read/:fiction_id',
        component:read,
        name:'read',
        // beforeEnter(to,from,next){
        //     let uid = window.localStorage.getItem('uid') || '';
        //     if(uid){
        //         next();
        //     }else{
        //         next('/login')
        //     }
        // }
    }
  ]
})

let arr = ['read'];

router.beforeEach((to,from,next) => {
    if(arr.indexOf(to.name) != -1){
        let uid = window.localStorage.getItem('uid') || '';
        if(uid){
            next();
        }else{
            next('/login')
        }
    }else{
        next();
    }
})

// 路由独享守卫   组建内守卫   全局守卫

export default router
